<?php
declare (strict_types=1);

namespace app\admin\service\market\ticket;

use app\admin\logic\market\ticket\Ticket as TicketLogic;
use app\common\service\BaseService;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Request;
use think\Response;

/**
 * 活动管理
 */
class Ticket extends BaseService
{
    /**
     * @Inject()
     *
     * @var TicketLogic
     */
    protected $ticketLogic;

    /**
     * 活动期号列表
     *
     * @throws DbException
     */
    public function ticketIssueNoList(int $pid): Response
    {
        $where = [
            ['pid', '<>', 0],
            ['pid', '=', $pid]
        ];
        Request::has('title', 'get', true) && $where[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        // Request::has('pid', 'get', true) && $where[] = ['pid', '=', Request::get('pid/d')];
        $data = $this->ticketLogic->getList($where);

        // 定义字段
        $keyList = [
            'issue_no'         => ListItem::make()->title('活动期号'),
            'target_user_type' => ListItem::make('custom')->title('目标用户'),
            'create_time'      => ListItem::make()->title('创建时间'),
            'update_time'      => ListItem::make()->title('最后更新时间'),
            'status'           => ListItem::make('custom')->title('启用状态'),
        ];
        // 设置表单
        $list = new PageList;
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * search
     *
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function search(): Response
    {
        $keyword = Request::get('keyword/s', '');
        $limit   = Request::get('limit/d', 15);
        $datas   = $this->ticketLogic->search($keyword, $limit);

        return $this->parseToData($datas, 1, '获取成功');
    }

    /**
     * 活动列表带分页
     *
     * @return Response
     * @throws DbException
     */
    public function ticketList(): Response
    {
        $where = [
            ['pid', '=', 0]
        ];
        // 活动名称
        Request::has('title', 'get', true) && $where[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        // 目标用户
        // Request::has('target_user_type', 'get', true) && $where[] = ['target_user_type', '=', Request::get('target_user_type/d')];
        $data = $this->ticketLogic->getList($where);

        return $this->parseToData($data, 1, '获取成功');
    }

    /**
     * info
     *
     * @param int $id
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function info(int $id): Response
    {
        $data = $this->ticketLogic->getInfoById($id);

        return $this->parseToData($data, 1, '获取成功');
    }
}
